<?php

namespace app\modules\Income\models;

use Yii;
use app\modules\Manage\models\User;

/**
 * This is the model class for table "income".
 *
 * @property int $id
 * @property string $company 公司
 * @property int $currency 货币(美元)
 * @property string $price 收入金额
 * @property int $status 1正常  2 禁用 
 * @property int $user_id 创建人ID
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class Income extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'income';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['currency', 'status', 'user_id'], 'integer'],
            [['price','real_price'], 'number','min'=>0],
            [['pro_price'], 'number'],
            [['price','company','real_price','pro_price'],'required'],
            [['created_at', 'updated_at','rate'], 'safe'],
            [['company','code'], 'string', 'max' => 128],
            [['remark'], 'string', 'max' => 1024],
            ['price','validatePrice'],
        ];
    }

    //保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){

            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;

            $t = 'INO';
            $d = date("Ymd");
            $count = Income::find()->where(['>=', 'created_at', date("Y-m-d 00:00:00")])->count();

            $num = sprintf("%04d", $count+1);

            $this->code = sprintf("%s%s%s", $t, $d, $num);
        }
        return parent::beforeSave($insert);

    }//}}}

     //限制金额大小
    public function validatePrice(){

        if($this->price < 0){
            $this->addError('price', '收入金额不能小于0');
        }

        $price = IncomeClaim::find()->where(['income_id'=>$this->id,'status'=>2])->sum('price');
        if($price){
            if($this->price < $price){
                 $this->addError('price', '收入金额不能小于已认领金额');
            }
        }
    }

    //获取录入人
    public function getUser(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    //获取认领单
    public function getIncomeClaim(){
        return $this->hasMany(IncomeClaim::className(), ['income_id' => 'id']);
    }
    //是否认领
    public function getIsClaim(){
        $model = IncomeClaim::find()->where(['income_id'=>$this->id])->all();
        if(!$model){
            return '未认领';
        }   
        $price = $this->getNotPrice();
        if($price <= 0 ){
            return '已认领';
        }
        if($price == $this->price){
            return '未认领';
        }else{
            return '部分认领';
        }
       
    }

    //已认领的金额
    public function getClaimPrice(){
        $price = IncomeClaim::find()->where(['income_id'=>$this->id,'status'=>2])->sum('price');
        return $price ? $price : 0;
    }

    //未认领余额
    public function getNotPrice(){
        $price = $this->price - $this->getClaimPrice();

        return $price ? $price : 0;
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'code' => Yii::t('app.income', 'Income Code'),
            'company' => Yii::t('app.income', 'Company'),
            'currency' => Yii::t('app', 'Currency'),
            'price' => Yii::t('app.income', 'Price'),
            'real_price' => Yii::t('app.income', 'Real Price'),
            'pro_price' => Yii::t('app.income', 'Pro Price'),
            'rate' => Yii::t('app.income', 'Rate'),
            'remark' => Yii::t('app.supplier', 'Remark'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
